package com.hhsj.redpoint;

import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;


import com.hhsj.redpoint.annos.Redpoint;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

public class RedpointUtil {
    private static final String TAG = "RedpointUtil";

    private final Map<String, Activity> registerActivity;

    private RedpointUtil() {
        registerActivity = new HashMap<>();
    }

    private final static RedpointUtil DEFAULT = new RedpointUtil();

    public static RedpointUtil getDefault() {
        return DEFAULT;
    }


    public void register(Activity activity) {
        String clazzName = activity.getClass().getName();
        if (!registerActivity.containsKey(clazzName)) {
            registerActivity.put(clazzName, activity);
        }
    }

    public void unRegister(Activity activity) {
        String clazzName = activity.getClass().getName();
        registerActivity.remove(clazzName);
    }

    public void showRedpoint(String tag) {
        long crrunteTime = System.currentTimeMillis();
        for (Activity activity : registerActivity.values()) {
            showRedpoint(tag, (ViewGroup) activity.findViewById(android.R.id.content));
        }
        Log.e(TAG, "showRedpoint time = " + (System.currentTimeMillis() - crrunteTime) + " ms");
    }


    public void removeRedpoint(String tag) {
        long crrunteTime = System.currentTimeMillis();
        for (Activity activity : registerActivity.values()) {
            removeRedpoint(tag, (ViewGroup) activity.findViewById(android.R.id.content));
        }
        Log.e(TAG, "removeRedpoint time = " + (System.currentTimeMillis() - crrunteTime) + " ms");
    }

    /**
     * @param tag       红点唯一标志
     * @param viewGroup 当前视图
     */
    public static void showRedpoint(String tag, ViewGroup viewGroup) {

        /**
         * 根布局中查找RedpointalbeLayout
         */
        if (viewGroup instanceof Redpointable) {
            redpoint(tag, viewGroup, true);
        } else {
            int childCount = viewGroup.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View view = viewGroup.getChildAt(i);
                //到RedpointableLayout 就停止处理
                if (view instanceof ViewGroup && view instanceof Redpointable) {
                    redpoint(tag, view, true);
                } else if (view instanceof ViewGroup) {
                    //迭代
                    showRedpoint(tag, (ViewGroup) view);
                }
            }
        }
    }

    /**
     * @param tag       红点唯一标志
     * @param viewGroup 当前视图
     */
    public static void removeRedpoint(String tag, ViewGroup viewGroup) {
        if (viewGroup instanceof Redpointable) {
            redpoint(tag, viewGroup, false);
        } else {
            int childCount = viewGroup.getChildCount();
            for (int i = 0; i < childCount; i++) {
                View view = viewGroup.getChildAt(i);
                //到RedpointableLayout 就停止处理
                if (view instanceof ViewGroup && view instanceof Redpointable) {
                    redpoint(tag, view, false);
                } else if (view instanceof ViewGroup) {
                    //迭代
                    removeRedpoint(tag, (ViewGroup) view);
                }
            }
        }
    }

    /**
     * @param tag  红点唯一标志
     * @param view 当前视图
     * @param show 显示还是移除
     */
    private static void redpoint(String tag, View view, boolean show) {
        Annotation annotation = view.getClass().getAnnotation(Redpoint.class);
        if (annotation != null) {
            Field[] field = view.getClass().getDeclaredFields();
            if (field.length == 0)
                return;
            for (Field field1 : field) {
                if (field1.getAnnotation(Redpoint.class) != null) {
                    try {
                        field1.setAccessible(true);
                        Object flag = field1.get(view);
                        if (flag instanceof Set) {
                            Set<String> flags = (Set<String>) flag;
                            if (flags.contains(tag)) {
                                if (show) {
                                    ((Redpointable) view).showRedpoint();
                                } else {
                                    ((Redpointable) view).removeRedpoint();
                                }
                            }
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

}
